using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace cb0tServer
{
    class CommandLevels
    {
        private static List<CommandLevelItem> items = new List<CommandLevelItem>();

        public enum LevelCodes : int
        {
            greetmsg = 0,
            pmgreetmsg = 1,
            idle = 2,
            anon = 3,
            vspy = 4,
            lastseen = 5,
            roominfo = 6,
            general = 7,
            sharefiles = 8,
            customnames = 9,
            caps = 10,
            stealth = 11,
            clock = 12,
            whois = 13,
            whowas = 14,
            move = 15,
            changename = 16,
            oldname = 17,
            kill = 18,
            ban = 19,
            unban = 20,
            listbans = 21,
            kiddy = 22,
            unkiddy = 23,
            muzzle = 24,
            unmuzzle = 25,
            rangeban = 26,
            rangeunban = 27,
            listrangebans = 28,
            cbans = 29,
            addtopic = 30,
            remtopic = 31,
            urltag = 32,
            echo = 33,
            unecho = 34,
            paint = 35,
            unpaint = 36,
            bansend = 37,
            ipsend = 38,
            logsend = 39,
            filterhelp = 40,
            filter = 41,
            roomsearch = 42,
            define = 43,
            urban = 44,
            wiki = 45,
            trace = 46,
            listlang = 47,
            translate = 48,
            untranslate = 49,
            admins = 50,
            adminmsg = 51,
            announce = 52,
            addquote = 53,
            quotesearch = 54,
            clone = 55,
            addkewltext = 56,
            remkewltext = 57,
            lower = 58,
            unlower = 59,
            stats = 60,
            colors = 61,
            mtimeout = 62,
            link = 63,
            unlink = 64,
            linkinfo = 65,
            banstats = 66,
            shout = 67,
            clear = 68,
            redirect = 69,
            durl = 70,
            dtopic = 71,
            quiz = 72,
            changemessage = 73,
            oldmessage = 74,
            disableavatar = 75
        };

        public static byte GetLevel(LevelCodes command)
        {
            return items[(int)command].command_level;
        }

        public static void SetLevel(String command_name, byte command_level)
        {
            foreach (CommandLevelItem item in items)
            {
                if (item.command_name == command_name)
                {
                    item.command_level = command_level;
                    return;
                }
            }
        }

        public static String[] GetCommands(byte command_level)
        {
            List<String> s = new List<String>();

            foreach (CommandLevelItem item in items)
                if (item.command_level == command_level)
                    s.Add(item.command_name);

            return s.Count == 0 ? null : s.ToArray();
        }

        public static void Load()
        {
            items.Clear();
            items.Add(new CommandLevelItem("#greetmsg", 3));
            items.Add(new CommandLevelItem("#pmgreetmsg", 3));
            items.Add(new CommandLevelItem("#idle", 3));
            items.Add(new CommandLevelItem("#anon", 3));
            items.Add(new CommandLevelItem("#vspy", 4));
            items.Add(new CommandLevelItem("#lastseen", 3));
            items.Add(new CommandLevelItem("#roominfo", 3));
            items.Add(new CommandLevelItem("#general", 2));
            items.Add(new CommandLevelItem("#sharefiles", 3));
            items.Add(new CommandLevelItem("#customnames", 2));
            items.Add(new CommandLevelItem("#caps", 3));
            items.Add(new CommandLevelItem("#stealth", 4));
            items.Add(new CommandLevelItem("#clock", 3));
            items.Add(new CommandLevelItem("#whois", 2));
            items.Add(new CommandLevelItem("#whowas", 2));
            items.Add(new CommandLevelItem("#move", 1));
            items.Add(new CommandLevelItem("#changename", 1));
            items.Add(new CommandLevelItem("#oldname", 1));
            items.Add(new CommandLevelItem("#kill", 1));
            items.Add(new CommandLevelItem("#ban", 2));
            items.Add(new CommandLevelItem("#unban", 2));
            items.Add(new CommandLevelItem("#listbans", 2));
            items.Add(new CommandLevelItem("#kiddy", 1));
            items.Add(new CommandLevelItem("#unkiddy", 1));
            items.Add(new CommandLevelItem("#muzzle", 1));
            items.Add(new CommandLevelItem("#unmuzzle", 1));
            items.Add(new CommandLevelItem("#rangeban", 2));
            items.Add(new CommandLevelItem("#rangeunban", 2));
            items.Add(new CommandLevelItem("#listrangebans", 2));
            items.Add(new CommandLevelItem("#cbans", 4));
            items.Add(new CommandLevelItem("#addtopic", 4));
            items.Add(new CommandLevelItem("#remtopic", 4));
            items.Add(new CommandLevelItem("#urltag", 4));
            items.Add(new CommandLevelItem("#echo", 1));
            items.Add(new CommandLevelItem("#unecho", 1));
            items.Add(new CommandLevelItem("#paint", 1));
            items.Add(new CommandLevelItem("#unpaint", 1));
            items.Add(new CommandLevelItem("#bansend", 3));
            items.Add(new CommandLevelItem("#ipsend", 3));
            items.Add(new CommandLevelItem("#logsend", 4));
            items.Add(new CommandLevelItem("#filterhelp", 3));
            items.Add(new CommandLevelItem("#filter", 3));
            items.Add(new CommandLevelItem("#roomsearch", 1));
            items.Add(new CommandLevelItem("#define", 1));
            items.Add(new CommandLevelItem("#urban", 1));
            items.Add(new CommandLevelItem("#wiki", 1));
            items.Add(new CommandLevelItem("#trace", 2));
            items.Add(new CommandLevelItem("#listlang", 1));
            items.Add(new CommandLevelItem("#translate", 1));
            items.Add(new CommandLevelItem("#untranslate", 1));
            items.Add(new CommandLevelItem("#admins", 1));
            items.Add(new CommandLevelItem("#adminmsg", 1));
            items.Add(new CommandLevelItem("#announce", 2));
            items.Add(new CommandLevelItem("#addquote", 1));
            items.Add(new CommandLevelItem("#quotesearch", 1));
            items.Add(new CommandLevelItem("#clone", 1));
            items.Add(new CommandLevelItem("#addkewltext", 1));
            items.Add(new CommandLevelItem("#remkewltext", 1));
            items.Add(new CommandLevelItem("#lower", 2));
            items.Add(new CommandLevelItem("#unlower", 2));
            items.Add(new CommandLevelItem("#stats", 3));
            items.Add(new CommandLevelItem("#colors", 4));
            items.Add(new CommandLevelItem("#mtimeout", 4));
            items.Add(new CommandLevelItem("#link", 4));
            items.Add(new CommandLevelItem("#unlink", 4));
            items.Add(new CommandLevelItem("#linkinfo", 2));
            items.Add(new CommandLevelItem("#banstats", 3));
            items.Add(new CommandLevelItem("#shout", 1));
            items.Add(new CommandLevelItem("#clearscreen", 2));
            items.Add(new CommandLevelItem("#redirect", 4));
            items.Add(new CommandLevelItem("#durl", 4));
            items.Add(new CommandLevelItem("#dtopic", 4));
            items.Add(new CommandLevelItem("#quiz", 1));
            items.Add(new CommandLevelItem("#changemessage", 1));
            items.Add(new CommandLevelItem("#oldmessage", 1));
            items.Add(new CommandLevelItem("#disableavatar", 2));

            try
            {
                FileStream f = new FileStream(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/levels.xml", FileMode.Open);
                XmlReader xml = XmlReader.Create(new StreamReader(f));

                xml.MoveToContent();
                xml.ReadSubtree().ReadToFollowing("levels");
                //items.Clear();

                while (xml.ReadToFollowing("level"))
                {
                    CommandLevelItem a = new CommandLevelItem();

                    xml.ReadToFollowing("name");
                    a.command_name = Encoding.UTF8.GetString(Convert.FromBase64String(xml.ReadElementContentAsString()));
                    xml.ReadToFollowing("id");
                    a.command_level = byte.Parse(xml.ReadElementContentAsString());

                    if (items.FindIndex(x => x.command_name == a.command_name) > -1)
                        items.Find(x => x.command_name == a.command_name).command_level = a.command_level;
                }

                xml.Close();
                f.Flush();
                f.Close();
                return;
            }
            catch { }

            try
            {
                File.Delete(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/levels.xml");
            }
            catch { }
        }

        public static void Save()
        {
            try
            {
                XmlWriterSettings appearance = new XmlWriterSettings();
                appearance.Indent = true;
                XmlWriter xml = XmlWriter.Create(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/levels.xml", appearance);

                xml.WriteStartDocument();
                xml.WriteStartElement("levels");

                foreach (CommandLevelItem a in items)
                {
                    xml.WriteStartElement("level");
                    xml.WriteElementString("name", Convert.ToBase64String(Encoding.UTF8.GetBytes(a.command_name)));
                    xml.WriteElementString("id", a.command_level.ToString());
                    xml.WriteEndElement();
                }

                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Flush();
                xml.Close();
            }
            catch { }
        }

    }

    class CommandLevelItem
    {
        public String command_name;
        public byte command_level;

        public CommandLevelItem() { }

        public CommandLevelItem(String command_name, byte command_level)
        {
            this.command_name = command_name;
            this.command_level = command_level;
        }
    }
}
